#!/usr/bin/python
# coding:utf-8
__author__ = 'good'
__createday__ = '2015-01-15'

from flask.ext.wtf import Form
from wtforms import TextField, TextAreaField, SelectField
from wtforms.validators import Length, DataRequired

from golsee.models import Article


class ArticleCreateForm(Form):
    id = TextField('article id')
    title = TextField(u'文章标题', [DataRequired(u'请输入标题！')])
    body = TextAreaField(u'文章内容',
                         [DataRequired(u'文章内容不能为空！')])
    category_id = SelectField(u'文章类别', coerce=int)
    tag = TextField(u'文章标签', [Length(max=20)])
    is_open = SelectField(u'是否公开', choices=[(0, u'不公开'), (0, u'公开')], coerce=int)

    def validate(self):
        if not Form.validate(self):
            return False
        category = Article.query.filter_by(title=self.title.data).first()
        if category != None:
            self.title.errors.append(u'文章标题不能重复！')
            return False
        return True


class ArticleEditForm(Form):
    id = TextField('article id', [DataRequired(u'请输入标题！')])
    title = TextField(u'文章标题', [DataRequired(u'请输入标题！')])
    body = TextAreaField(u'文章内容',
                         [DataRequired(u'文章内容不能为空！')])
    category_id = SelectField(u'文章类别', coerce=int)
    tag = TextField(u'文章标签', [Length(max=20)])
    is_open = SelectField(u'是否公开', choices=[(0, u'不公开'), (1, u'公开')], coerce=int)





